<template>
	<view class="login-container">
		<!-- 登陆图标 -->
		<uni-icons type="contact-filled" size="100" color="#ffaa00"></uni-icons>

		<!-- 获得用户授权 -->
		<button type="primary" class="btn-login" @click="handleNormalLogin">一键登录</button>
		<view class="tips-text" style="color:#233;">登陆后享受更多权益</view>

	</view>


</template>

<script>
	export default {
		data() {
			return {
				// isWeixinMiniProgram: false, // 是否微信小程序环境
				// code: '', // 微信登录临时code
				// userInfo: null // 用户信息
			};
		},
		onLoad() {
			this.checkEnvironment();
		},
		methods: {
			checkEnvironment() {
				const systemInfo = uni.getSystemInfoSync();
				this.isWeixinMiniProgram = systemInfo.platform === 'devtools' || systemInfo.platform === 'mp-weixin';
			},

			getWxCode() {
				return new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success: (res) => {
							if (res.code) {
								this.code = res.code;
								resolve(res.code);
							} else {
								reject(new Error('获取微信code失败'));
							}
						},
						fail: (err) => {
							console.error('微信登录失败:', err);
							reject(new Error('微信登录失败'));
							uni.showToast({
								title: '微信登录失败',
								icon: 'none'
							});
						}
					});
				});
			},
			getWxUserInfo() {
				return new Promise((resolve, reject) => {
					uni.getUserInfo({
						provider: 'weixin',
						success: (res) => {
							console.log(res.userInfo)
							resolve(res.userInfo);
						},
						fail: (err) => {
							reject(new Error('获取用户信息失败'));
						}
					});
				});
			},

			handleLoginSuccess(loginData) {
				if (!loginData.data || !loginData.data.token) {
					throw new Error('登录成功但未获取到token');
				}
				uni.setStorageSync('token', loginData.data.token);

				//  保存用户信息（
				const userInfo = loginData.data.user;
				uni.setStorageSync('userInfo', userInfo);
				const pages = getCurrentPages();
				if (pages.length > 1) {
					uni.navigateBack();
				} else {
					uni.switchTab({
						url: '/pages/category/index'
					});
				}
			},


			async handleNormalLogin() {
				const code = await this.getWxCode();
				const userInfo = await this.getWxUserInfo();

				const loginApiRes = await this.$http.post('/login', {
					code: code,
					// nickname: userInfo.nickName,
					// avatar: userInfo.avatarUrl

				});
				this.handleLoginSuccess(loginApiRes);
			}
		}
	};
</script>

<style lang="scss">
	.login-container {
		height: 750rpx;
		background-color: #ffffff;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		position: relative;
		overflow: hidden; //隐藏椭圆的下半部分也就是超出容器的部分

		&::after {
			content: ' ';
			display: block;
			width: 100%;
			height: 40rpx;
			background-color: #f8f8f8;
			position: absolute;
			bottom: 0;
			left: 0;
			border-radius: 100%;
			transform: translateY(50%);
		}

		.btn-login {
			color: #ffffff;
			width: 90%;
			height: 80rpx;
			border-radius: 60rpx;
			background-color: #ffaa00;
			margin: 15rpx 0;
		}

		.tips-text {
			color: #c6c6bd;
			font-size: 24rpx;
			margin-top: 30rpx;
		}
	}
</style>